package com.dp.temp;

import com.alibaba.fastjson.JSONObject;
import com.dp.io.ESN;
import com.dp.io.JSONUtil;
import com.dp.logUtil.SetMsg;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping({"aaa"})
public class erweima {


    @PostMapping({"bbb"})
    public JSONObject addAppGrade(@RequestBody Map<String, Object> map) {
        try {
            Iterator var2 = map.keySet().iterator();

            while(var2.hasNext()) {
                String a = (String)var2.next();
                this.getURCodePicture(a);
            }
            return JSONUtil.SUCCESSJson((Object)null, "100", "请求成功");
        } catch (Exception var4) {
            var4.printStackTrace();
            return JSONUtil.ErrorJson((Object)null, "500", "请求错误");
        }
    }

    public String getURCodePicture(String content) {
        int width = 600;
        int height = 600;
        String type = "png";
        HashMap hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        hints.put(EncodeHintType.MARGIN, 2);

        try {
            BitMatrix bitMatrix = (new MultiFormatWriter()).encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            String path = "E:\\QuicklyTools\\EWM\\" + content + ".png";
//            Path file = (new File(path)).toPath();
            // 创建一个新的BufferedImage，大小是二维码大小加上文本空间
            BufferedImage qrCodeImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
            BufferedImage combinedImage = new BufferedImage(qrCodeImage.getWidth(), qrCodeImage.getHeight() + 50, BufferedImage.TYPE_INT_RGB); // 假设文本高度为50像素

            // 绘制二维码到新的BufferedImage
            Graphics2D g2d = combinedImage.createGraphics();
            g2d.drawImage(qrCodeImage, 0, 0, null);

            // 设置文本字体和颜色
            Font font = new Font("仿宋", Font.PLAIN, 40); // 使用合适的字体和大小
            g2d.setFont(font);
            g2d.setColor(Color.white); // 设置文本颜色

            // 计算文本位置（例如，在二维码下方中央）
            FontMetrics fm = g2d.getFontMetrics();
            int textX = (combinedImage.getWidth() - fm.stringWidth(content)) / 2;
            int textY = combinedImage.getHeight()- fm.getDescent();

            // 绘制文本
            g2d.drawString(content, textX, textY);

            // 释放Graphics2D资源
            g2d.dispose();

            // 保存图片
            Path file = new File(path).toPath();
            ImageIO.write(combinedImage, type, file.toFile());

            return path;
        } catch (Exception var9) {
            var9.printStackTrace();
            return "生成失败";
        }
    }
}
